Imports Aricie.DNN.Services.Url.FriendlyUrl.EntityClasses
Imports SD.LLBLGen.Pro.ORMSupportClasses

Namespace Aricie.DNN.Services.Url.FriendlyUrl.Data


    Public Class FupPersistenceJob

        Private Shared _FupCommitOrder As List(Of UnitOfWorkBlockType)

        Public Function IsEmpty() As Boolean
            Return _MappingsToInsert.Count = 0 AndAlso Me._MappingsToUpdate.Count = 0 AndAlso Me._MappingsToDelete.Count = 0 AndAlso Me._PatternsToInsert.Count = 0
        End Function

        Private Shared ReadOnly Property FupCommitOrder() As List(Of UnitOfWorkBlockType)
            Get
                If _FupCommitOrder Is Nothing Then
                    _FupCommitOrder = New List(Of UnitOfWorkBlockType)
                    _FupCommitOrder.Add(UnitOfWorkBlockType.Deletes)
                    _FupCommitOrder.Add(UnitOfWorkBlockType.DeletesPerformedDirectly)
                    _FupCommitOrder.Add(UnitOfWorkBlockType.Inserts)
                    _FupCommitOrder.Add(UnitOfWorkBlockType.Updates)
                    _FupCommitOrder.Add(UnitOfWorkBlockType.UpdatesPerformedDirectly)
                End If
                Return _FupCommitOrder
            End Get
        End Property


        Private objLock As New Object()

        Private _MappingsToInsert As New Dictionary(Of String, FriendlyUrlMapping)
        Private _MappingsToUpdate As New Dictionary(Of String, FriendlyUrlMapping)
        Private _MappingsToDelete As New Dictionary(Of String, Boolean)
        Private _PatternsToInsert As New Dictionary(Of String, FriendlyUrlMapping)

        Public Function InsertMapping(ByVal objMapping As FriendlyUrlMapping) As Boolean

            If _MappingsToInsert.ContainsKey(objMapping.FriendlyUri.AbsoluteUri) Then
                Return False
            Else
                SyncLock objLock
                    _MappingsToInsert.Add(objMapping.FriendlyUri.AbsoluteUri, objMapping)
                End SyncLock
            End If
            Return True
        End Function

        Public Function UpdateMapping(ByVal objMapping As FriendlyUrlMapping) As Boolean

            If _MappingsToUpdate.ContainsKey(objMapping.FriendlyUri.AbsoluteUri) Then
                Return False
            Else
                SyncLock objLock
                    _MappingsToUpdate.Add(objMapping.FriendlyUri.AbsoluteUri, objMapping)
                End SyncLock
            End If
            Return True
        End Function

        Public Function DeleteMapping(ByVal friendlyUrl As String) As Boolean

            If _MappingsToDelete.ContainsKey(friendlyUrl) Then
                Return False
            Else
                SyncLock objLock
                    _MappingsToDelete.Add(friendlyUrl, True)
                End SyncLock
            End If
            Return True
        End Function

        Public Function InsertPattern(ByVal objMapping As FriendlyUrlMapping) As Boolean

            If _PatternsToInsert.ContainsKey(objMapping.Pattern) Then
                Return False
            Else
                SyncLock objLock
                    _PatternsToInsert.Add(objMapping.Pattern, objMapping)
                End SyncLock
            End If
            Return True
        End Function




        Public Function Commit(ByVal objAdapter As DataAccessAdapterBase) As DbOperations
            Dim toReturn As DbOperations

            'Dim objMappingsToInsert, objMappingsToUpdate, objMappingsToDelete, objPatternsToInsert As Dictionary(Of String, FriendlyUrlMapping)
            SyncLock objLock
                Dim tempUOW As New UnitOfWork2(FupCommitOrder)
                'objMappingsToInsert = New Dictionary(Of String, FriendlyUrlMapping)(Me._MappingsToInsert)
                'objMappingsToUpdate = New Dictionary(Of String, FriendlyUrlMapping)(Me._MappingsToUpdate)
                'objMappingsToDelete = New Dictionary(Of String, FriendlyUrlMapping)(Me._MappingsToDelete)
                'objPatternsToInsert = New Dictionary(Of String, FriendlyUrlMapping)(Me._PatternsToInsert)
                Me.BuildUOW(tempUOW)
                toReturn = New DbOperations(tempUOW)

                Try
                    toReturn.NbDbOps = tempUOW.Commit(objAdapter, True)
                Catch llblEx As ORMQueryExecutionException
                    DotNetNuke.Services.Exceptions.Exceptions.LogException(New ApplicationException(String.Format("Following Query raised an exception: {0}", llblEx.QueryExecuted), llblEx))
                Catch llblEx As ORMConcurrencyException
                    Dim mapping As FupUrlMappingEntity = DirectCast(llblEx.EntityWhichFailed, FupUrlMappingEntity)
                    If mapping IsNot Nothing Then
                        DotNetNuke.Services.Exceptions.Exceptions.LogException(New ApplicationException(String.Format("Saving/Deleting the following mapping raised an exception: Friendly Url: {0} - Real Url: {1}", mapping.FriendlyUrl, mapping.RealUrl), llblEx))
                    Else
                        DotNetNuke.Services.Exceptions.Exceptions.LogException(llblEx)
                    End If
                Catch ex As Exception
                    DotNetNuke.Services.Exceptions.Exceptions.LogException(ex)
                End Try

                Me._MappingsToInsert.Clear()
                Me._MappingsToUpdate.Clear()
                Me._MappingsToDelete.Clear()
                Me._PatternsToInsert.Clear()
            End SyncLock

            Return toReturn

        End Function


        Private Sub BuildUOW(ByRef objUOW As UnitOfWork2)
            For Each objMapping As FriendlyUrlMapping In Me._MappingsToInsert.Values
                Me.SaveMapping(objUOW, objMapping, True)
            Next
            For Each objMapping As FriendlyUrlMapping In Me._MappingsToUpdate.Values
                Me.SaveMapping(objUOW, objMapping, False)
            Next
            For Each strFriendlyUrl As String In Me._MappingsToDelete.Keys
                Dim toDelete As New FupUrlMappingEntity(strFriendlyUrl)
                toDelete.IsNew = False
                objUOW.AddForDelete(toDelete)
            Next
            For Each objMapping As FriendlyUrlMapping In Me._PatternsToInsert.Values
                Dim patternMappingEntity As New FupPatternMappingEntity(objMapping.Pattern)
                patternMappingEntity.FriendlyUrl = objMapping.FriendlyUri.AbsoluteUri
                objUOW.AddForSave(patternMappingEntity)
            Next
        End Sub

        Private Sub SaveMapping(ByRef obUOW As UnitOfWork2, ByVal objMapping As FriendlyUrlMapping, ByVal isNew As Boolean)
            Dim mapping As New FupUrlMappingEntity(objMapping.FriendlyUri.AbsoluteUri)
            mapping.RealUrl = objMapping.OriginalUri.AbsoluteUri
            mapping.CreatedDate = DateTime.Now
            mapping.IsNew = isNew
            obUOW.AddForSave(mapping)
        End Sub

        ''Modification CGA - Gestion des portails enfants
        'Private Sub CheckChildUrl(ByRef objMapping As FriendlyUrlMapping)

        'End Sub


    End Class
End NameSpace